package com.bjsxt.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class AliPhoneUtil {
    private static Integer newCode;
    //获取四位随机验证码
    public static void setNewCode(){
        int code= (int) Math.floor(Math.random()*9000+1000);
        newCode = code;
    }
    //返回四位随机验证码
    public static Integer getNewCode(){
        return newCode;
    }
    //发送手机验证码
    public static SendSmsResponse sendSms(String phonenumer,Integer newCode) throws ClientException{
        //初始化ascClient需要的几个参数
        final String product = "Dysmsapi";
        final String domain = "dysmsapi.aliyuncs.com";
        //替换成你的AK
        final String accessKeyId = "LTAI5tKHeu3NYQe8SyPs9sfz";//accesskeyID
        final String accessKeySecret = "A1xPrwURMaCTSonTdT9HGTxgYeC4im";//accesskeySecret
        //初始化assClient，暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou","cn-hangzhou",product,domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        //必填：待发送手机号
        request.setPhoneNumbers(phonenumer);
        //必填：短信签名
        request.setSignName("ting域主持人平台");
        //必填：短信模块
        request.setTemplateCode("MissingPhoneNumbers");
        //可选：模块中的变量替换JSON传
        //如果需要带换行符，请参照JSON协议兑换行的要求
        request.setTemplateParam("{\"code\":\""+newCode+"\"}");
        //请求失败这里会跑遗产
        SendSmsResponse sendSmsResponse=acsClient.getAcsResponse(request);
        return sendSmsResponse;


    }
}
